package me.sdevil507.biz.fruit.service.impl;

import lombok.extern.slf4j.Slf4j;
import me.sdevil507.biz.fruit.dto.FruitWxAppletDTO;
import me.sdevil507.biz.fruit.po.FruitWxAppletPO;
import me.sdevil507.biz.fruit.repository.FruitWxAppletRepository;
import me.sdevil507.biz.fruit.service.FruitWxAppletService;
import me.sdevil507.supports.jpa.PageParamsDTO;
import me.sdevil507.supports.jpa.SortParamsDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class FruitWxAppletServiceImpl implements FruitWxAppletService {

    @Autowired
    FruitWxAppletRepository fruitWxAppletRepository;

    @Override
    public void save(FruitWxAppletPO fruitWxAppletPO) {
        fruitWxAppletRepository.save(fruitWxAppletPO);
    }

    @Override
    public Page<FruitWxAppletPO> getWxAppletFruitOfPage(FruitWxAppletDTO searchDto, PageParamsDTO pageDto, SortParamsDTO sortDto) {
        Sort sort;
        if (null == sortDto) {
            sort = Sort.by(Sort.Direction.DESC, "createdDate");
        } else {
            // 排序
            sort = sortDto.getSort();
            if (null == sort) {
                sort = Sort.by(Sort.Direction.DESC, "createdDate");
            }
        }

        // 执行返回
        Pageable pageable = pageDto.getPageableWithSort(sort);

        return fruitWxAppletRepository.findAll(pageable);
    }
}
